package com.example.shishaolong.warehousemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shishaolong.warehousemanager.common.DateUtil;
import com.example.shishaolong.warehousemanager.common.SystemConst;
import com.example.shishaolong.warehousemanager.dao.GoodsDao;
import com.example.shishaolong.warehousemanager.entity.GoodsEntity;
import com.example.shishaolong.warehousemanager.view.TitleLayout;

public class GoodsAddActivity extends AppCompatActivity {

    private GoodsDao goodsDao;
    private TitleLayout titleLayout;
    private String gotoFrom;
    private GoodsEntity goodsEntity;

    /*
    思路：获取intent传值，
        判断是  查看修改  还是新增。
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_add);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }

        //界面绑定
        final TextView goodsName = (TextView)findViewById(R.id.goots_waste_name_value);
        final TextView receivePerson = (TextView)findViewById(R.id.receive_person_value);
        final TextView buyerName = (TextView)findViewById(R.id.buyer_name_value);
        final TextView storageDate = (TextView)findViewById(R.id.storage_date_value);
        final TextView modifyDate = (TextView)findViewById(R.id.modify_date_value);
        final TextView describel= (TextView)findViewById(R.id.goods_describle_value);


        //创建数据实体
        goodsDao = new GoodsDao(getApplicationContext());
        final Intent intent = getIntent();
        gotoFrom = intent.getStringExtra(SystemConst.GOODS_GOTO_FROM);
        if(SystemConst.GOODS_GOTO_FROM_UPDATE.equals(gotoFrom)){  //修改，需要给页面组件写入值
            int id = intent.getExtras().getInt(SystemConst.GOODS_ENTITY_ID);
            goodsEntity = goodsDao.findById(id);
            //向界面文本框中注入数据
            goodsName.setText(goodsEntity.getName());
            receivePerson.setText(goodsEntity.getReceivePerson());
            buyerName.setText(goodsEntity.getBuyerName());
            storageDate.setText(goodsEntity.getStorageDate());
            modifyDate.setText(goodsEntity.getModifyDate());
            describel.setText(goodsEntity.getDescribe());
        }else { //添加  创建货物实体类
            goodsEntity = new GoodsEntity();
            storageDate.setText(DateUtil.getCurrentDateStr());
            modifyDate.setText(DateUtil.getCurrentDateStr());
        }

        //设置标题栏
        titleLayout = (TitleLayout) findViewById(R.id.title_layout);
        titleLayout.setTitle("货物信息");
        titleLayout.setRightButton(new TitleLayout.ButotonEntity("保存", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //封装数据实体
                goodsEntity.setName(goodsName.getText().toString());
                goodsEntity.setReceivePerson(receivePerson.getText().toString());
                goodsEntity.setBuyerName(buyerName.getText().toString());
                goodsEntity.setStorageDate(storageDate.getText().toString());
//                goodsEntity.setModifyDate(modifyDate.getText().toString());
                goodsEntity.setDescribe(describel.getText().toString());

                //保存货物信息：分为两种入库和修改的  根据tid判断
                if(SystemConst.GOODS_GOTO_FROM_STORAGE.equals(gotoFrom)){
                    goodsDao.add(goodsEntity);
                }else{
                    goodsDao.update(goodsEntity);
                }
                Toast.makeText(getApplicationContext(),"保存成功！",Toast.LENGTH_SHORT).show();

                //返回到货物列表主界面，指定返回结果
                Intent gotoGoodsActivityIntent = new Intent();//返回到货物列表的intent
                setResult(SystemConst.GOODS_ADD_ACTIVITY_START_FOR_RESULT,gotoGoodsActivityIntent); //设置页面标识符

                finish();
            }
        }));


    }
}
